\documentclass[11pt]{article}

    \usepackage[breakable]{tcolorbox}
    \usepackage{parskip} % Stop auto-indenting (to mimic markdown behaviour)
    
    \usepackage{iftex}
    \ifPDFTeX
    	\usepackage[T1]{fontenc}
    	\usepackage{mathpazo}
    \else
    	\usepackage{fontspec}
    \fi

    % Basic figure setup, for now with no caption control since it's done
    % automatically by Pandoc (which extracts ![](path) syntax from Markdown).
    \usepackage{graphicx}
    % Maintain compatibility with old templates. Remove in nbconvert 6.0
    \let\Oldincludegraphics\includegraphics
    % Ensure that by default, figures have no caption (until we provide a
    % proper Figure object with a Caption API and a way to capture that
    % in the conversion process - todo).
    \usepackage{caption}
    \DeclareCaptionFormat{nocaption}{}
    \captionsetup{format=nocaption,aboveskip=0pt,belowskip=0pt}

    \usepackage{float}
    \floatplacement{figure}{H} % forces figures to be placed at the correct location
    \usepackage{xcolor} % Allow colors to be defined
    \usepackage{enumerate} % Needed for markdown enumerations to work
    \usepackage{geometry} % Used to adjust the document margins
    \usepackage{amsmath} % Equations
    \usepackage{amssymb} % Equations
    \usepackage{textcomp} % defines textquotesingle
    % Hack from http://tex.stackexchange.com/a/47451/13684:
    \AtBeginDocument{%
        \def\PYZsq{\textquotesingle}% Upright quotes in Pygmentized code
    }
    \usepackage{upquote} % Upright quotes for verbatim code
    \usepackage{eurosym} % defines \euro
    \usepackage[mathletters]{ucs} % Extended unicode (utf-8) support
    \usepackage{fancyvrb} % verbatim replacement that allows latex
    \usepackage{grffile} % extends the file name processing of package graphics 
                         % to support a larger range
    \makeatletter % fix for old versions of grffile with XeLaTeX
    \@ifpackagelater{grffile}{2019/11/01}
    {
      % Do nothing on new versions
    }
    {
      \def\Gread@@xetex#1{%
        \IfFileExists{"\Gin@base".bb}%
        {\Gread@eps{\Gin@base.bb}}%
        {\Gread@@xetex@aux#1}%
      }
    }
    \makeatother
    \usepackage[Export]{adjustbox} % Used to constrain images to a maximum size
    \adjustboxset{max size={0.9\linewidth}{0.9\paperheight}}

    % The hyperref package gives us a pdf with properly built
    % internal navigation ('pdf bookmarks' for the table of contents,
    % internal cross-reference links, web links for URLs, etc.)
    \usepackage{hyperref}
    % The default LaTeX title has an obnoxious amount of whitespace. By default,
    % titling removes some of it. It also provides customization options.
    \usepackage{titling}
    \usepackage{longtable} % longtable support required by pandoc >1.10
    \usepackage{booktabs}  % table support for pandoc > 1.12.2
    \usepackage[inline]{enumitem} % IRkernel/repr support (it uses the enumerate* environment)
    \usepackage[normalem]{ulem} % ulem is needed to support strikethroughs (\sout)
                                % normalem makes italics be italics, not underlines
    \usepackage{mathrsfs}
    

    
    % Colors for the hyperref package
    \definecolor{urlcolor}{rgb}{0,.145,.698}
    \definecolor{linkcolor}{rgb}{.71,0.21,0.01}
    \definecolor{citecolor}{rgb}{.12,.54,.11}

    % ANSI colors
    \definecolor{ansi-black}{HTML}{3E424D}
    \definecolor{ansi-black-intense}{HTML}{282C36}
    \definecolor{ansi-red}{HTML}{E75C58}
    \definecolor{ansi-red-intense}{HTML}{B22B31}
    \definecolor{ansi-green}{HTML}{00A250}
    \definecolor{ansi-green-intense}{HTML}{007427}
    \definecolor{ansi-yellow}{HTML}{DDB62B}
    \definecolor{ansi-yellow-intense}{HTML}{B27D12}
    \definecolor{ansi-blue}{HTML}{208FFB}
    \definecolor{ansi-blue-intense}{HTML}{0065CA}
    \definecolor{ansi-magenta}{HTML}{D160C4}
    \definecolor{ansi-magenta-intense}{HTML}{A03196}
    \definecolor{ansi-cyan}{HTML}{60C6C8}
    \definecolor{ansi-cyan-intense}{HTML}{258F8F}
    \definecolor{ansi-white}{HTML}{C5C1B4}
    \definecolor{ansi-white-intense}{HTML}{A1A6B2}
    \definecolor{ansi-default-inverse-fg}{HTML}{FFFFFF}
    \definecolor{ansi-default-inverse-bg}{HTML}{000000}

    % common color for the border for error outputs.
    \definecolor{outerrorbackground}{HTML}{FFDFDF}

    % commands and environments needed by pandoc snippets
    % extracted from the output of `pandoc -s`
    \providecommand{\tightlist}{%
      \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
    \DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}}
    % Add ',fontsize=\small' for more characters per line
    \newenvironment{Shaded}{}{}
    \newcommand{\KeywordTok}[1]{\textcolor[rgb]{0.00,0.44,0.13}{\textbf{{#1}}}}
    \newcommand{\DataTypeTok}[1]{\textcolor[rgb]{0.56,0.13,0.00}{{#1}}}
    \newcommand{\DecValTok}[1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}}
    \newcommand{\BaseNTok}[1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}}
    \newcommand{\FloatTok}[1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}}
    \newcommand{\CharTok}[1]{\textcolor[rgb]{0.25,0.44,0.63}{{#1}}}
    \newcommand{\StringTok}[1]{\textcolor[rgb]{0.25,0.44,0.63}{{#1}}}
    \newcommand{\CommentTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textit{{#1}}}}
    \newcommand{\OtherTok}[1]{\textcolor[rgb]{0.00,0.44,0.13}{{#1}}}
    \newcommand{\AlertTok}[1]{\textcolor[rgb]{1.00,0.00,0.00}{\textbf{{#1}}}}
    \newcommand{\FunctionTok}[1]{\textcolor[rgb]{0.02,0.16,0.49}{{#1}}}
    \newcommand{\RegionMarkerTok}[1]{{#1}}
    \newcommand{\ErrorTok}[1]{\textcolor[rgb]{1.00,0.00,0.00}{\textbf{{#1}}}}
    \newcommand{\NormalTok}[1]{{#1}}
    
    % Additional commands for more recent versions of Pandoc
    \newcommand{\ConstantTok}[1]{\textcolor[rgb]{0.53,0.00,0.00}{{#1}}}
    \newcommand{\SpecialCharTok}[1]{\textcolor[rgb]{0.25,0.44,0.63}{{#1}}}
    \newcommand{\VerbatimStringTok}[1]{\textcolor[rgb]{0.25,0.44,0.63}{{#1}}}
    \newcommand{\SpecialStringTok}[1]{\textcolor[rgb]{0.73,0.40,0.53}{{#1}}}
    \newcommand{\ImportTok}[1]{{#1}}
    \newcommand{\DocumentationTok}[1]{\textcolor[rgb]{0.73,0.13,0.13}{\textit{{#1}}}}
    \newcommand{\AnnotationTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textbf{\textit{{#1}}}}}
    \newcommand{\CommentVarTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textbf{\textit{{#1}}}}}
    \newcommand{\VariableTok}[1]{\textcolor[rgb]{0.10,0.09,0.49}{{#1}}}
    \newcommand{\ControlFlowTok}[1]{\textcolor[rgb]{0.00,0.44,0.13}{\textbf{{#1}}}}
    \newcommand{\OperatorTok}[1]{\textcolor[rgb]{0.40,0.40,0.40}{{#1}}}
    \newcommand{\BuiltInTok}[1]{{#1}}
    \newcommand{\ExtensionTok}[1]{{#1}}
    \newcommand{\PreprocessorTok}[1]{\textcolor[rgb]{0.74,0.48,0.00}{{#1}}}
    \newcommand{\AttributeTok}[1]{\textcolor[rgb]{0.49,0.56,0.16}{{#1}}}
    \newcommand{\InformationTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textbf{\textit{{#1}}}}}
    \newcommand{\WarningTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textbf{\textit{{#1}}}}}
    
    
    % Define a nice break command that doesn't care if a line doesn't already
    % exist.
    \def\br{\hspace*{\fill} \\* }
    % Math Jax compatibility definitions
    \def\gt{>}
    \def\lt{<}
    \let\Oldtex\TeX
    \let\Oldlatex\LaTeX
    \renewcommand{\TeX}{\textrm{\Oldtex}}
    \renewcommand{\LaTeX}{\textrm{\Oldlatex}}
    % Document parameters
    % Document title
    \title{ex\_2\_4}
    
    
    
    
    
% Pygments definitions
\makeatletter
\def\PY@reset{\let\PY@it=\relax \let\PY@bf=\relax%
    \let\PY@ul=\relax \let\PY@tc=\relax%
    \let\PY@bc=\relax \let\PY@ff=\relax}
\def\PY@tok#1{\csname PY@tok@#1\endcsname}
\def\PY@toks#1+{\ifx\relax#1\empty\else%
    \PY@tok{#1}\expandafter\PY@toks\fi}
\def\PY@do#1{\PY@bc{\PY@tc{\PY@ul{%
    \PY@it{\PY@bf{\PY@ff{#1}}}}}}}
\def\PY#1#2{\PY@reset\PY@toks#1+\relax+\PY@do{#2}}

\@namedef{PY@tok@w}{\def\PY@tc##1{\textcolor[rgb]{0.73,0.73,0.73}{##1}}}
\@namedef{PY@tok@c}{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}}
\@namedef{PY@tok@cp}{\def\PY@tc##1{\textcolor[rgb]{0.74,0.48,0.00}{##1}}}
\@namedef{PY@tok@k}{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\@namedef{PY@tok@kp}{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\@namedef{PY@tok@kt}{\def\PY@tc##1{\textcolor[rgb]{0.69,0.00,0.25}{##1}}}
\@namedef{PY@tok@o}{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\@namedef{PY@tok@ow}{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.67,0.13,1.00}{##1}}}
\@namedef{PY@tok@nb}{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\@namedef{PY@tok@nf}{\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}}
\@namedef{PY@tok@nc}{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}}
\@namedef{PY@tok@nn}{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}}
\@namedef{PY@tok@ne}{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.82,0.25,0.23}{##1}}}
\@namedef{PY@tok@nv}{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}}
\@namedef{PY@tok@no}{\def\PY@tc##1{\textcolor[rgb]{0.53,0.00,0.00}{##1}}}
\@namedef{PY@tok@nl}{\def\PY@tc##1{\textcolor[rgb]{0.63,0.63,0.00}{##1}}}
\@namedef{PY@tok@ni}{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.60,0.60,0.60}{##1}}}
\@namedef{PY@tok@na}{\def\PY@tc##1{\textcolor[rgb]{0.49,0.56,0.16}{##1}}}
\@namedef{PY@tok@nt}{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\@namedef{PY@tok@nd}{\def\PY@tc##1{\textcolor[rgb]{0.67,0.13,1.00}{##1}}}
\@namedef{PY@tok@s}{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\@namedef{PY@tok@sd}{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\@namedef{PY@tok@si}{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.73,0.40,0.53}{##1}}}
\@namedef{PY@tok@se}{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.73,0.40,0.13}{##1}}}
\@namedef{PY@tok@sr}{\def\PY@tc##1{\textcolor[rgb]{0.73,0.40,0.53}{##1}}}
\@namedef{PY@tok@ss}{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}}
\@namedef{PY@tok@sx}{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\@namedef{PY@tok@m}{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\@namedef{PY@tok@gh}{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,0.50}{##1}}}
\@namedef{PY@tok@gu}{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.50,0.00,0.50}{##1}}}
\@namedef{PY@tok@gd}{\def\PY@tc##1{\textcolor[rgb]{0.63,0.00,0.00}{##1}}}
\@namedef{PY@tok@gi}{\def\PY@tc##1{\textcolor[rgb]{0.00,0.63,0.00}{##1}}}
\@namedef{PY@tok@gr}{\def\PY@tc##1{\textcolor[rgb]{1.00,0.00,0.00}{##1}}}
\@namedef{PY@tok@ge}{\let\PY@it=\textit}
\@namedef{PY@tok@gs}{\let\PY@bf=\textbf}
\@namedef{PY@tok@gp}{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,0.50}{##1}}}
\@namedef{PY@tok@go}{\def\PY@tc##1{\textcolor[rgb]{0.53,0.53,0.53}{##1}}}
\@namedef{PY@tok@gt}{\def\PY@tc##1{\textcolor[rgb]{0.00,0.27,0.87}{##1}}}
\@namedef{PY@tok@err}{\def\PY@bc##1{{\setlength{\fboxsep}{-\fboxrule}\fcolorbox[rgb]{1.00,0.00,0.00}{1,1,1}{\strut ##1}}}}
\@namedef{PY@tok@kc}{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\@namedef{PY@tok@kd}{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\@namedef{PY@tok@kn}{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\@namedef{PY@tok@kr}{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\@namedef{PY@tok@bp}{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\@namedef{PY@tok@fm}{\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}}
\@namedef{PY@tok@vc}{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}}
\@namedef{PY@tok@vg}{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}}
\@namedef{PY@tok@vi}{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}}
\@namedef{PY@tok@vm}{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}}
\@namedef{PY@tok@sa}{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\@namedef{PY@tok@sb}{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\@namedef{PY@tok@sc}{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\@namedef{PY@tok@dl}{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\@namedef{PY@tok@s2}{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\@namedef{PY@tok@sh}{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\@namedef{PY@tok@s1}{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\@namedef{PY@tok@mb}{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\@namedef{PY@tok@mf}{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\@namedef{PY@tok@mh}{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\@namedef{PY@tok@mi}{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\@namedef{PY@tok@il}{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\@namedef{PY@tok@mo}{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\@namedef{PY@tok@ch}{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}}
\@namedef{PY@tok@cm}{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}}
\@namedef{PY@tok@cpf}{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}}
\@namedef{PY@tok@c1}{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}}
\@namedef{PY@tok@cs}{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}}

\def\PYZbs{\char`\\}
\def\PYZus{\char`\_}
\def\PYZob{\char`\{}
\def\PYZcb{\char`\}}
\def\PYZca{\char`\^}
\def\PYZam{\char`\&}
\def\PYZlt{\char`\<}
\def\PYZgt{\char`\>}
\def\PYZsh{\char`\#}
\def\PYZpc{\char`\%}
\def\PYZdl{\char`\$}
\def\PYZhy{\char`\-}
\def\PYZsq{\char`\'}
\def\PYZdq{\char`\"}
\def\PYZti{\char`\~}
% for compatibility with earlier versions
\def\PYZat{@}
\def\PYZlb{[}
\def\PYZrb{]}
\makeatother


    % For linebreaks inside Verbatim environment from package fancyvrb. 
    \makeatletter
        \newbox\Wrappedcontinuationbox 
        \newbox\Wrappedvisiblespacebox 
        \newcommand*\Wrappedvisiblespace {\textcolor{red}{\textvisiblespace}} 
        \newcommand*\Wrappedcontinuationsymbol {\textcolor{red}{\llap{\tiny$\m@th\hookrightarrow$}}} 
        \newcommand*\Wrappedcontinuationindent {3ex } 
        \newcommand*\Wrappedafterbreak {\kern\Wrappedcontinuationindent\copy\Wrappedcontinuationbox} 
        % Take advantage of the already applied Pygments mark-up to insert 
        % potential linebreaks for TeX processing. 
        %        {, <, #, %, $, ' and ": go to next line. 
        %        _, }, ^, &, >, - and ~: stay at end of broken line. 
        % Use of \textquotesingle for straight quote. 
        \newcommand*\Wrappedbreaksatspecials {% 
            \def\PYGZus{\discretionary{\char`\_}{\Wrappedafterbreak}{\char`\_}}% 
            \def\PYGZob{\discretionary{}{\Wrappedafterbreak\char`\{}{\char`\{}}% 
            \def\PYGZcb{\discretionary{\char`\}}{\Wrappedafterbreak}{\char`\}}}% 
            \def\PYGZca{\discretionary{\char`\^}{\Wrappedafterbreak}{\char`\^}}% 
            \def\PYGZam{\discretionary{\char`\&}{\Wrappedafterbreak}{\char`\&}}% 
            \def\PYGZlt{\discretionary{}{\Wrappedafterbreak\char`\<}{\char`\<}}% 
            \def\PYGZgt{\discretionary{\char`\>}{\Wrappedafterbreak}{\char`\>}}% 
            \def\PYGZsh{\discretionary{}{\Wrappedafterbreak\char`\#}{\char`\#}}% 
            \def\PYGZpc{\discretionary{}{\Wrappedafterbreak\char`\%}{\char`\%}}% 
            \def\PYGZdl{\discretionary{}{\Wrappedafterbreak\char`\$}{\char`\$}}% 
            \def\PYGZhy{\discretionary{\char`\-}{\Wrappedafterbreak}{\char`\-}}% 
            \def\PYGZsq{\discretionary{}{\Wrappedafterbreak\textquotesingle}{\textquotesingle}}% 
            \def\PYGZdq{\discretionary{}{\Wrappedafterbreak\char`\"}{\char`\"}}% 
            \def\PYGZti{\discretionary{\char`\~}{\Wrappedafterbreak}{\char`\~}}% 
        } 
        % Some characters . , ; ? ! / are not pygmentized. 
        % This macro makes them "active" and they will insert potential linebreaks 
        \newcommand*\Wrappedbreaksatpunct {% 
            \lccode`\~`\.\lowercase{\def~}{\discretionary{\hbox{\char`\.}}{\Wrappedafterbreak}{\hbox{\char`\.}}}% 
            \lccode`\~`\,\lowercase{\def~}{\discretionary{\hbox{\char`\,}}{\Wrappedafterbreak}{\hbox{\char`\,}}}% 
            \lccode`\~`\;\lowercase{\def~}{\discretionary{\hbox{\char`\;}}{\Wrappedafterbreak}{\hbox{\char`\;}}}% 
            \lccode`\~`\:\lowercase{\def~}{\discretionary{\hbox{\char`\:}}{\Wrappedafterbreak}{\hbox{\char`\:}}}% 
            \lccode`\~`\?\lowercase{\def~}{\discretionary{\hbox{\char`\?}}{\Wrappedafterbreak}{\hbox{\char`\?}}}% 
            \lccode`\~`\!\lowercase{\def~}{\discretionary{\hbox{\char`\!}}{\Wrappedafterbreak}{\hbox{\char`\!}}}% 
            \lccode`\~`\/\lowercase{\def~}{\discretionary{\hbox{\char`\/}}{\Wrappedafterbreak}{\hbox{\char`\/}}}% 
            \catcode`\.\active
            \catcode`\,\active 
            \catcode`\;\active
            \catcode`\:\active
            \catcode`\?\active
            \catcode`\!\active
            \catcode`\/\active 
            \lccode`\~`\~ 	
        }
    \makeatother

    \let\OriginalVerbatim=\Verbatim
    \makeatletter
    \renewcommand{\Verbatim}[1][1]{%
        %\parskip\z@skip
        \sbox\Wrappedcontinuationbox {\Wrappedcontinuationsymbol}%
        \sbox\Wrappedvisiblespacebox {\FV@SetupFont\Wrappedvisiblespace}%
        \def\FancyVerbFormatLine ##1{\hsize\linewidth
            \vtop{\raggedright\hyphenpenalty\z@\exhyphenpenalty\z@
                \doublehyphendemerits\z@\finalhyphendemerits\z@
                \strut ##1\strut}%
        }%
        % If the linebreak is at a space, the latter will be displayed as visible
        % space at end of first line, and a continuation symbol starts next line.
        % Stretch/shrink are however usually zero for typewriter font.
        \def\FV@Space {%
            \nobreak\hskip\z@ plus\fontdimen3\font minus\fontdimen4\font
            \discretionary{\copy\Wrappedvisiblespacebox}{\Wrappedafterbreak}
            {\kern\fontdimen2\font}%
        }%
        
        % Allow breaks at special characters using \PYG... macros.
        \Wrappedbreaksatspecials
        % Breaks at punctuation characters . , ; ? ! and / need catcode=\active 	
        \OriginalVerbatim[#1,codes*=\Wrappedbreaksatpunct]%
    }
    \makeatother

    % Exact colors from NB
    \definecolor{incolor}{HTML}{303F9F}
    \definecolor{outcolor}{HTML}{D84315}
    \definecolor{cellborder}{HTML}{CFCFCF}
    \definecolor{cellbackground}{HTML}{F7F7F7}
    
    % prompt
    \makeatletter
    \newcommand{\boxspacing}{\kern\kvtcb@left@rule\kern\kvtcb@boxsep}
    \makeatother
    \newcommand{\prompt}[4]{
        {\ttfamily\llap{{\color{#2}[#3]:\hspace{3pt}#4}}\vspace{-\baselineskip}}
    }
    

    
    % Prevent overflowing lines due to hard-to-break entities
    \sloppy 
    % Setup hyperref package
    \hypersetup{
      breaklinks=true,  % so long urls are correctly broken across lines
      colorlinks=true,
      urlcolor=urlcolor,
      linkcolor=linkcolor,
      citecolor=citecolor,
      }
    % Slightly bigger margins than the latex defaults
    
    \geometry{verbose,tmargin=1in,bmargin=1in,lmargin=1in,rmargin=1in}
    
    

\begin{document}
    
    \maketitle
    
    

    
    \hypertarget{ux4e60ux9898-2.4}{%
\subsection{习题 2.4}\label{ux4e60ux9898-2.4}}

    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{1}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{n}{data} \PY{o}{\PYZlt{}\PYZhy{}} \PY{n+nf}{read.table}\PY{p}{(}\PY{l+s}{\PYZdq{}}\PY{l+s}{./ex\PYZus{}2\PYZus{}4.txt\PYZdq{}}\PY{p}{,} \PY{n}{header}\PY{o}{=}\PY{k+kc}{TRUE}\PY{p}{)}
\PY{n}{data}
\end{Verbatim}
\end{tcolorbox}

    A data.frame: 15 × 3
\begin{tabular}{lll}
 Y & X1 & X2\\
 <int> & <int> & <int>\\
\hline
	 162 & 274 & 2450\\
	 120 & 180 & 3254\\
	 223 & 375 & 3802\\
	 131 & 205 & 2838\\
	  67 &  86 & 2347\\
	 169 & 265 & 3782\\
	  81 &  98 & 3008\\
	 192 & 330 & 2450\\
	 116 & 195 & 2137\\
	  55 &  53 & 2560\\
	 252 & 430 & 4020\\
	 232 & 372 & 4427\\
	 144 & 236 & 2660\\
	 103 & 157 & 2088\\
	 212 & 370 & 2605\\
\end{tabular}


    
    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{2}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{n+nf}{attach}\PY{p}{(}\PY{n}{data}\PY{p}{)}
\end{Verbatim}
\end{tcolorbox}

    回归之前应该先看一下变量之间的相关关系如何，可以借助\emph{散点图矩阵}来实现。

散点图矩阵（scatterplot matrix):
每个行与列的交叉点所在的散点图表示其所在的行与列的两个变量的相关关系：

    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{3}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{n+nf}{pairs}\PY{p}{(}\PY{n}{data}\PY{p}{)}
\end{Verbatim}
\end{tcolorbox}

    \begin{center}
    \adjustimage{max size={0.9\linewidth}{0.9\paperheight}}{output_4_0.png}
    \end{center}
    { \hspace*{\fill} \\}
    
    用 psych 包里的 \texttt{pairs.panels} 可以作出有更多信息的图：

    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{4}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{n+nf}{library}\PY{p}{(}\PY{n}{psych}\PY{p}{)}
\PY{n+nf}{pairs.panels}\PY{p}{(}\PY{n}{data}\PY{p}{)}
\end{Verbatim}
\end{tcolorbox}

    \begin{center}
    \adjustimage{max size={0.9\linewidth}{0.9\paperheight}}{output_6_0.png}
    \end{center}
    { \hspace*{\fill} \\}
    
    \begin{itemize}
\tightlist
\item
  对角线上方：相关系数
\item
  对角线：每个特征的数值分布直方图
\item
  下方：散点图

  \begin{itemize}
  \tightlist
  \item
    相关椭圆：

    \begin{itemize}
    \tightlist
    \item
      中心点：两个变量的均值所确定的点
    \item
      椭圆形状：两个变量的相关性：椭圆越被拉伸，其相关性越强
    \end{itemize}
  \item
    局部回归平滑曲线：x轴和y轴变量之间的一般关系
  \end{itemize}
\end{itemize}

    假设 \(Y\) 与 \(X_1\), \(X_2\) 之间满足线性回归关系

\[
y_i=\beta_0+\beta_1x_{i1}+\beta_2x_{i2}+\epsilon_i,\quad i=1,2,\cdots,15
\]

其中 \(\epsilon_i (i=1,1,\cdots,15)\) 独立同分布于 \(N(0,\sigma^2)\).

    \hypertarget{section}{%
\subsubsection{(1)}\label{section}}

求回归系数 \(\beta_0,\beta_1,\beta_2\) 的最小二乘估计和误差方差
\(\sigma^2\) 的估计， 写出回归方程并对回归系数作解释

    回归：

    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{5}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{n}{fm} \PY{o}{\PYZlt{}\PYZhy{}} \PY{n+nf}{lm}\PY{p}{(}\PY{n}{Y} \PY{o}{\PYZti{}} \PY{n}{X1} \PY{o}{+} \PY{n}{X2}\PY{p}{,} \PY{n}{data}\PY{p}{)}
\PY{n+nf}{summary}\PY{p}{(}\PY{n}{fm}\PY{p}{)}
\end{Verbatim}
\end{tcolorbox}

    
    \begin{Verbatim}[commandchars=\\\{\}]

Call:
lm(formula = Y \textasciitilde{} X1 + X2, data = data)

Residuals:
    Min      1Q  Median      3Q     Max 
-3.8320 -1.2044 -0.2406  1.4888  3.3092 

Coefficients:
             Estimate Std. Error t value Pr(>|t|)    
(Intercept) 3.4526128  2.4306505   1.420    0.181    
X1          0.4960050  0.0060544  81.924  < 2e-16 ***
X2          0.0091991  0.0009681   9.502  6.2e-07 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 2.177 on 12 degrees of freedom
Multiple R-squared:  0.9989,	Adjusted R-squared:  0.9988 
F-statistic:  5679 on 2 and 12 DF,  p-value: < 2.2e-16

    \end{Verbatim}

    
    Coefficients 是回归得到的系数，由此得到回归方程：

\[
\hat Y = 0.496005 X_1 + 0.009199 X_2 + 3.452613
\]

Residual standard error 即残差标准差，\(\sigma=2.177\),
由此得到误差方差的估计：

\[
\sigma^2=4.739
\]

    \hypertarget{section}{%
\subsubsection{(2)}\label{section}}

求出方差分析表，解释对现行回归关系显著性检验的结果。求复相关系数的平方
\(R^2\) 的值并解释其意义

模型方差分析表:

    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{6}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{n+nf}{anova}\PY{p}{(}\PY{n}{fm}\PY{p}{)}
\end{Verbatim}
\end{tcolorbox}

    A anova: 3 × 5
\begin{tabular}{r|lllll}
  & Df & Sum Sq & Mean Sq & F value & Pr(>F)\\
  & <int> & <dbl> & <dbl> & <dbl> & <dbl>\\
\hline
	X1 &  1 & 53416.71863 & 53416.718634 & 11268.64354 & 3.270351e-19\\
	X2 &  1 &   427.99780 &   427.997800 &    90.28923 & 6.201181e-07\\
	Residuals & 12 &    56.88357 &     4.740297 &          NA &           NA\\
\end{tabular}


    
    aov 输出更友好，可以直接显示出结果：

    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{7}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{n+nf}{summary}\PY{p}{(}\PY{n+nf}{aov}\PY{p}{(}\PY{n}{fm}\PY{p}{)}\PY{p}{)}
\end{Verbatim}
\end{tcolorbox}

    
    \begin{Verbatim}[commandchars=\\\{\}]
            Df Sum Sq Mean Sq  F value  Pr(>F)    
X1           1  53417   53417 11268.64 < 2e-16 ***
X2           1    428     428    90.29 6.2e-07 ***
Residuals   12     57       5                     
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
    \end{Verbatim}

    
    \(R^2\) 可以由 summary(fm) 输出的 \texttt{Multiple\ R-squared} 看出：

    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{8}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{n+nf}{summary}\PY{p}{(}\PY{n}{fm}\PY{p}{)}\PY{o}{\PYZdl{}}\PY{n}{r.squared}
\end{Verbatim}
\end{tcolorbox}

    0.998944677605876

    
    \(\therefore R^2=0.9989\)

    \hypertarget{section}{%
\subsubsection{(3)}\label{section}}

分别求 \(\beta_1\) 和 \(\beta_2\) 的置信度为 \(95\%\) 的置信区间

    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{9}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{c+c1}{\PYZsh{} 模型参数的置信区间}
\PY{n+nf}{confint}\PY{p}{(}\PY{n}{fm}\PY{p}{,} \PY{n}{level} \PY{o}{=} \PY{l+m}{0.95}\PY{p}{)}
\end{Verbatim}
\end{tcolorbox}

    A matrix: 3 × 2 of type dbl
\begin{tabular}{r|ll}
  & 2.5 \% & 97.5 \%\\
\hline
	(Intercept) & -1.843319690 & 8.74854527\\
	X1 &  0.482813482 & 0.50919647\\
	X2 &  0.007089742 & 0.01130842\\
\end{tabular}


    
    得到所求置信区间：

\[
\begin{aligned}
\beta_1:&& (0.482813482, 0.50919647)\\
\beta_2:&& (0.007089742, 0.01130842)
\end{aligned}
\]

    \hypertarget{section}{%
\subsubsection{(4)}\label{section}}

对 \(\alpha=0.05\)，分别检验人数 \(X_1\) 和收入 \(X_2\) 对销量 \(Y\)
的影响是否显著， 利用回归系数有关的一般假设检验方法检验 \(X_1\) 和
\(X_2\) 的交互作用 (\(X_1X_2\)) 对 \(Y\) 的影响是否显著。

    在 \texttt{summary(fm)} 输出的 Coefficients 段即可看出 \(X_1\)、\(X_2\)
对 \(Y\) 影响的显著性：

    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{10}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{n+nf}{summary}\PY{p}{(}\PY{n}{fm}\PY{p}{)}\PY{c+c1}{\PYZsh{}\PYZdl{}coefficients}
\end{Verbatim}
\end{tcolorbox}

    
    \begin{Verbatim}[commandchars=\\\{\}]

Call:
lm(formula = Y \textasciitilde{} X1 + X2, data = data)

Residuals:
    Min      1Q  Median      3Q     Max 
-3.8320 -1.2044 -0.2406  1.4888  3.3092 

Coefficients:
             Estimate Std. Error t value Pr(>|t|)    
(Intercept) 3.4526128  2.4306505   1.420    0.181    
X1          0.4960050  0.0060544  81.924  < 2e-16 ***
X2          0.0091991  0.0009681   9.502  6.2e-07 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 2.177 on 12 degrees of freedom
Multiple R-squared:  0.9989,	Adjusted R-squared:  0.9988 
F-statistic:  5679 on 2 and 12 DF,  p-value: < 2.2e-16

    \end{Verbatim}

    
    Coefficients 段输出有:

\begin{itemize}
\tightlist
\item
  \texttt{t\ value}: T 检验的值
\item
  \texttt{Pr(\textgreater{}\textbar{}t\textbar{})}: 表示 T 检验判定 P
  值，后面有显著性标记（\texttt{*}号）
\item
  显著性标记：\texttt{*} 个数对应显著性水平。
\end{itemize}

这里 \(X_1\)、\(X_2\) 都是有 \texttt{***} 的，或者看 P 都在 0.0001
以下， 所以对于 \(\alpha=0.05\)，认为人数 \(X_1\) 和收入 \(X_2\) 对销量
\(Y\) 的影响显著。

    注：这里可以做约简模型，求\(SSE(R)\)、\(SSE(T)\)\ldots{}
去分析，下面的函数会有帮助：（由于麻烦，这里不采用这种方法。）

    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{11}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{n}{sse} \PY{o}{\PYZlt{}\PYZhy{}} \PY{n+nf}{function}\PY{p}{(}\PY{n}{model}\PY{p}{,} \PY{n}{y}\PY{p}{)} \PY{n+nf}{sum}\PY{p}{(}\PY{p}{(}\PY{n+nf}{fitted}\PY{p}{(}\PY{n}{model}\PY{p}{)} \PY{o}{\PYZhy{}} \PY{n}{y}\PY{p}{)}\PY{o}{\PYZca{}}\PY{l+m}{2}\PY{p}{)}
\PY{n}{ssr} \PY{o}{\PYZlt{}\PYZhy{}} \PY{n+nf}{function}\PY{p}{(}\PY{n}{model}\PY{p}{,} \PY{n}{y}\PY{p}{)} \PY{n+nf}{sum}\PY{p}{(}\PY{p}{(}\PY{n+nf}{fitted}\PY{p}{(}\PY{n}{model}\PY{p}{)} \PY{o}{\PYZhy{}} \PY{n+nf}{mean}\PY{p}{(}\PY{n}{y}\PY{p}{)}\PY{p}{)}\PY{o}{\PYZca{}}\PY{l+m}{2}\PY{p}{)}
\PY{n}{sst} \PY{o}{\PYZlt{}\PYZhy{}} \PY{n+nf}{function}\PY{p}{(}\PY{n}{model}\PY{p}{,} \PY{n}{y}\PY{p}{)} \PY{n+nf}{ssr}\PY{p}{(}\PY{n}{model}\PY{p}{,} \PY{n}{y}\PY{p}{)} \PY{o}{+} \PY{n+nf}{sse}\PY{p}{(}\PY{n}{model}\PY{p}{,} \PY{n}{y}\PY{p}{)}

\PY{n}{ss} \PY{o}{\PYZlt{}\PYZhy{}} \PY{n+nf}{function}\PY{p}{(}\PY{n}{model}\PY{p}{,} \PY{n}{y}\PY{p}{)} \PY{p}{\PYZob{}}
    \PY{n+nf}{cbind}\PY{p}{(}\PY{n+nf}{matrix}\PY{p}{(}\PY{n+nf}{c}\PY{p}{(}\PY{l+s}{\PYZdq{}}\PY{l+s}{sse\PYZdq{}}\PY{p}{,} \PY{l+s}{\PYZdq{}}\PY{l+s}{ssr\PYZdq{}}\PY{p}{,} \PY{l+s}{\PYZdq{}}\PY{l+s}{sst\PYZdq{}}\PY{p}{)}\PY{p}{,} \PY{l+m}{3}\PY{p}{,} \PY{l+m}{1}\PY{p}{)}\PY{p}{,} 
          \PY{n+nf}{c}\PY{p}{(}\PY{n+nf}{sse}\PY{p}{(}\PY{n}{model}\PY{p}{,} \PY{n}{y}\PY{p}{)}\PY{p}{,} \PY{n+nf}{ssr}\PY{p}{(}\PY{n}{model}\PY{p}{,} \PY{n}{y}\PY{p}{)}\PY{p}{,} \PY{n+nf}{sst}\PY{p}{(}\PY{n}{model}\PY{p}{,} \PY{n}{y}\PY{p}{)}\PY{p}{)}\PY{p}{)}
    \PY{p}{\PYZcb{}}

\PY{c+c1}{\PYZsh{} R\PYZca{}2}
\PY{c+c1}{\PYZsh{} ssr(fm, Y)/sst(fm, Y)}
\end{Verbatim}
\end{tcolorbox}

    下面研究 \(X_1\)、\(X_2\) 交互作用对 Y 的影响：

    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{12}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{n}{fm1} \PY{o}{\PYZlt{}\PYZhy{}} \PY{n+nf}{update}\PY{p}{(}\PY{n}{fm}\PY{p}{,} \PY{n}{.} \PY{o}{\PYZti{}} \PY{n}{.} \PY{o}{+} \PY{n}{X1}\PY{o}{:}\PY{n}{X2}\PY{p}{)}
\PY{n+nf}{summary}\PY{p}{(}\PY{n}{fm1}\PY{p}{)}
\end{Verbatim}
\end{tcolorbox}

    
    \begin{Verbatim}[commandchars=\\\{\}]

Call:
lm(formula = Y \textasciitilde{} X1 + X2 + X1:X2, data = data)

Residuals:
    Min      1Q  Median      3Q     Max 
-3.9094 -1.2010 -0.1811  1.5072  3.2141 

Coefficients:
             Estimate Std. Error t value Pr(>|t|)    
(Intercept) 4.901e+00  8.539e+00   0.574    0.578    
X1          4.911e-01  2.832e-02  17.344 2.45e-09 ***
X2          8.674e-03  3.124e-03   2.777    0.018 *  
X1:X2       1.698e-06  9.556e-06   0.178    0.862    
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 2.271 on 11 degrees of freedom
Multiple R-squared:  0.9989,	Adjusted R-squared:  0.9987 
F-statistic:  3481 on 3 and 11 DF,  p-value: < 2.2e-16

    \end{Verbatim}

    
    从拟合的结果里，\texttt{X1:X2} 的 P-value=0.862 比较大，说明交互作用对 Y
的影响不显著，没有必要引入交叉项。

    \hypertarget{section}{%
\subsubsection{(5)}\label{section}}

\includegraphics{https://tva1.sinaimg.cn/large/008i3skNly1gr3yf8l5dzj30mh01taai.jpg}

    新数据：

    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{13}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{n}{x01} \PY{o}{=} \PY{l+m}{220}
\PY{n}{x02} \PY{o}{=} \PY{l+m}{2500}
\PY{n}{newdata} \PY{o}{\PYZlt{}\PYZhy{}} \PY{n+nf}{data.frame}\PY{p}{(}\PY{n}{X1} \PY{o}{=} \PY{n+nf}{c}\PY{p}{(}\PY{n}{x01}\PY{p}{)}\PY{p}{,} \PY{n}{X2} \PY{o}{=} \PY{n+nf}{c}\PY{p}{(}\PY{n}{x02}\PY{p}{)}\PY{p}{)}
\PY{n}{newdata}
\end{Verbatim}
\end{tcolorbox}

    A data.frame: 1 × 2
\begin{tabular}{ll}
 X1 & X2\\
 <dbl> & <dbl>\\
\hline
	 220 & 2500\\
\end{tabular}


    
    代入模型进行预测：

    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{14}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{n+nf}{predict}\PY{p}{(}\PY{n}{fm}\PY{p}{,} \PY{n}{newdata}\PY{p}{)}
\end{Verbatim}
\end{tcolorbox}

    \textbf{1:} 135.571409702671

    
    要获取置信区间需要再传入几个参数：

    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{15}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{n+nf}{predict}\PY{p}{(}\PY{n}{fm}\PY{p}{,} \PY{n}{newdata}\PY{p}{,} \PY{n}{interval}\PY{o}{=}\PY{l+s}{\PYZdq{}}\PY{l+s}{prediction\PYZdq{}}\PY{p}{,} \PY{n}{levels}\PY{o}{=}\PY{l+m}{0.95}\PY{p}{)}
\end{Verbatim}
\end{tcolorbox}

    A matrix: 1 × 3 of type dbl
\begin{tabular}{r|lll}
  & fit & lwr & upr\\
\hline
	1 & 135.5714 & 130.5998 & 140.543\\
\end{tabular}


    
    即得到预测值 \(\hat y_0 = 135.5714\)，置信度为 \(95\%\) 的置信区间
\((130.5998, 140.543)\)。

    \hypertarget{section}{%
\subsubsection{(6)}\label{section}}

\includegraphics{https://tva1.sinaimg.cn/large/008i3skNly1gr3yz2huq0j30mm02naax.jpg}

    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{16}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{c+c1}{\PYZsh{} 拟合值：}
\PY{n}{fm.fitted} \PY{o}{\PYZlt{}\PYZhy{}} \PY{n+nf}{fitted}\PY{p}{(}\PY{n}{fm}\PY{p}{)}
\PY{c+c1}{\PYZsh{} 残差：}
\PY{n}{fm.residuals} \PY{o}{\PYZlt{}\PYZhy{}} \PY{n+nf}{residuals}\PY{p}{(}\PY{n}{fm}\PY{p}{)}
\PY{c+c1}{\PYZsh{} 学生化残差：}
\PY{n}{fm.rstudent} \PY{o}{\PYZlt{}\PYZhy{}} \PY{n+nf}{rstudent}\PY{p}{(}\PY{n}{fm}\PY{p}{)}

\PY{n}{fmr} \PY{o}{\PYZlt{}\PYZhy{}} \PY{n+nf}{data.frame}\PY{p}{(}
    \PY{n}{`拟合值`}\PY{o}{=}\PY{n}{fm.fitted}\PY{p}{,} 
    \PY{n}{`残差`}\PY{o}{=}\PY{n}{fm.residuals}\PY{p}{,} 
    \PY{n}{`学生化残差`}\PY{o}{=}\PY{n}{fm.rstudent}
\PY{p}{)}\PY{p}{;} \PY{n}{fmr}
\end{Verbatim}
\end{tcolorbox}

    A data.frame: 15 × 3
\begin{tabular}{r|lll}
  & 拟合值 & 残差 & 学生化残差\\
  & <dbl> & <dbl> & <dbl>\\
\hline
	1 & 161.89572 &  0.1042756 &  0.04973473\\
	2 & 122.66732 & -2.6673176 & -1.36670170\\
	3 & 224.42938 & -1.4293843 & -0.71265041\\
	4 & 131.24062 & -0.2406244 & -0.11000544\\
	5 &  67.69928 & -0.6992835 & -0.34443368\\
	6 & 169.68486 & -0.6848553 & -0.33365032\\
	7 &  79.73194 &  1.2680643 &  0.65018013\\
	8 & 189.67200 &  2.3279970 &  1.25776219\\
	9 & 119.83202 & -3.8320189 & -2.21655274\\
	10 &  53.29052 &  1.7094765 &  0.91079624\\
	11 & 253.71506 & -1.7150576 & -0.92397237\\
	12 & 228.69079 &  3.3092051 &  2.16085046\\
	13 & 144.97934 & -0.9793423 & -0.45379850\\
	14 & 100.53307 &  2.4669251 &  1.27498091\\
	15 & 210.93806 &  1.0619404 &  0.55945704\\
\end{tabular}


    
    对于学生化残差用频率检验法：

    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{17}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{n+nf}{hist}\PY{p}{(}\PY{n}{fm.rstudent}\PY{p}{)}
\PY{n+nf}{rug}\PY{p}{(}\PY{n}{fm.rstudent}\PY{p}{)}
\end{Verbatim}
\end{tcolorbox}

    \begin{center}
    \adjustimage{max size={0.9\linewidth}{0.9\paperheight}}{output_43_0.png}
    \end{center}
    { \hspace*{\fill} \\}
    
    \begin{itemize}
\tightlist
\item
  有 \(\frac{10}{15}\approx 0.68\) 落在 \((-1.0,1.0)\) 内，
\item
  有 \(\frac{13}{15}\approx 0.87\) 落在 \((-1.5,1.5)\) 内，
\item
  有 \(\frac{15}{15} = 1.00\) 落在 \((-2.4,2.4)\) 内，
\end{itemize}

可见，学生化残差与上述个区间的频率在 \(N(0,1)\)
分布的相应概率相差不大，所以模型误差项的正态性假设是合理的。

进一步，可以做一个 ks 检测：

    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{18}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{n+nf}{ks.test}\PY{p}{(}\PY{n}{fm.rstudent}\PY{p}{,} \PY{l+s}{\PYZdq{}}\PY{l+s}{pnorm\PYZdq{}}\PY{p}{,} \PY{l+m}{0}\PY{p}{,} \PY{l+m}{1}\PY{p}{)}
\end{Verbatim}
\end{tcolorbox}

    
    \begin{Verbatim}[commandchars=\\\{\}]

	One-sample Kolmogorov-Smirnov test

data:  fm.rstudent
D = 0.11208, p-value = 0.9808
alternative hypothesis: two-sided

    \end{Verbatim}

    
    检测结果也说明正态分布。

    下面作出各种残差图：

    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{19}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{n+nf}{par}\PY{p}{(}\PY{n}{mfrow}\PY{o}{=}\PY{n+nf}{c}\PY{p}{(}\PY{l+m}{2}\PY{p}{,}\PY{l+m}{2}\PY{p}{)}\PY{p}{)}

\PY{n+nf}{plot}\PY{p}{(}\PY{n}{fm}\PY{p}{)}
\end{Verbatim}
\end{tcolorbox}

    \begin{center}
    \adjustimage{max size={0.9\linewidth}{0.9\paperheight}}{output_48_0.png}
    \end{center}
    { \hspace*{\fill} \\}
    
    \begin{itemize}
\tightlist
\item
  左上图：\textbf{残差-拟合}：残差和拟合值之间，数据点均匀分布在y=0两侧，呈现出随机的分布，没有明显的形状特征，说明残差数据表现比较好。
\item
  右上图：\textbf{标准化残差
  Q-Q}：数据点按对角直线排列，趋于一条直线，并被对角直接穿过，直观上符合正态分布。
\item
  左下图：\textbf{标准化残差-拟合}：数据随机分布与左上图蕾丝，无明显的形状特征。
\item
  右下图：\textbf{标准化残差杠杆图}：可以看出离群点、高杠杆值点和强影响点。（这里不做讨论）
\end{itemize}

由这些图，可以认为相应的线性回归模型以及误差的独立正态分布的假设是合理的。

    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{20}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{n+nf}{detach}\PY{p}{(}\PY{n}{data}\PY{p}{)}
\end{Verbatim}
\end{tcolorbox}


    % Add a bibliography block to the postdoc
    
    
    
\end{document}
